package com.sfhk.dao.impl;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import com.sfhk.dao.UserDao;
import com.sfhk.domain.User;
import com.sfhk.utils.C3P0Utils;

import java.util.List;

public class UserDaoImpl implements UserDao {

    private JdbcTemplate template = new JdbcTemplate(C3P0Utils.getDateSource());
    @Override
    public List<User> findAll() {
        String sql = "select * from user";
        List<User> list = template.query(sql,new BeanPropertyRowMapper<>(User.class));
        return list;
    }

    @Override
    public User findByUserName(String username) {
        String sql = "select * from user where username = ?";
        User user = template.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), username);
        return user;
    }

    @Override
    public void deleteById(int id) {
        String sql = "delete from user where uid = ?";
        template.update(sql,id);
    }

    @Override
    public void addUser(User user) {
        String sql = "insert into user values(null,?,?,?,?,?,?,null,null)";
        template.update(sql,user.getuName(),user.getGender(),user.getAge(),user.getNavite(),user.getQq(),user.getEmail());
    }

    @Override
    public void update(User user) {
        String sql = "update user set gender=?, age=?, navite=?,qq=?,email=? where uid = ?";
        template.update(sql, user.getGender(),user.getAge(),user.getNavite(),user.getQq(),user.getEmail(),user.getuId());
    }

    @Override
    public User findById(int id) {
        String sql = "select * from user where uid = ?";
        return template.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), id);
    }


    public static void main(String[] args) {
        List<User> list = new UserDaoImpl().findAll();
        list.forEach(System.out::println);
    }
}
